/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package EntityPack;

import MapPack.Cell;
import MaterialPack.IPortable;
import MapPack.Map;
import ToolPack.FluidContainer;
import ToolPack.PortableContainer;
import ToolPack.SpaceSuit;
import com.badlogic.gdx.graphics.Texture;

import java.util.ArrayList;

/**
 *
 * @author Aaron
 */

public class Player extends Entity implements IWalk {
    private SpaceSuit suit;
    private float oxygenConsumptionRate = 0.003f;
    private float oxygenToCarbonDioxideConversionRatio = 1.2f;
    private boolean alive = true;
    private PortableContainer backpack;
    private int backpackSize = 5;
    private static Texture texture = new Texture("player32.png");
    
    public Player (Map map, Cell position) {
        super(map, position);
        this.suit = new SpaceSuit();
        this.backpack = new PortableContainer(backpackSize);
        this.containers.addAll(this.suit.getContainers());
    }

    public boolean moveRight ( ) {
        return this.setX(this.getX() + 1);
    }

    public boolean moveLeft ( ) {
        return this.setX(this.getX() - 1);
    }

    public boolean moveUp ( ) {
        return this.setY(this.getY() + 1);
    }

    public boolean moveDown ( ) {
        return this.setY(this.getY() - 1);
    }

    private void breathe ( ) {
        float breath = this.suit.useOxygen(this.oxygenConsumptionRate);
        if (breath < this.oxygenConsumptionRate) {
            this.die();
        }
        this.suit.addCarbonDioxide(oxygenToCarbonDioxideConversionRatio * breath);
    }

    public void performBodilyFunctions ( )
    {
        this.suit.performSystemFunctions();
        breathe();
    }

    public boolean isAlive ( ) {
        return this.alive;
    }

    public void die ( ) {
        this.alive = false;
    }

    public ArrayList<IPortable> getInventory ( ) {
        return this.backpack.getInventory();
    }

    public boolean addPortable (IPortable portable) {
        return this.backpack.addPortable(portable);
    }

    public boolean removePortable (IPortable portable) {
        return this.backpack.removePortable(portable);
    }

    public boolean pickup (Cell selectedCell) {

        boolean acquired = false;


        ArrayList<Entity> toRemoveFromMap = new ArrayList<>();
        for (Entity entity : selectedCell.getEntities()) {
            if (entity instanceof IPortable) {
                if (this.addPortable((IPortable) entity)) {
                    toRemoveFromMap.add(entity);
                    acquired = true;
                }
            }
        }

        for (Entity entity : toRemoveFromMap) {
            entity.removeFromMap();
        }

        return acquired;
    }

    public boolean place (IPortable portable, Cell selectedCell) {
        boolean placed = false;

        if (selectedCell != null) {
            if (portable instanceof Entity) {
                if (this.getInventory().contains(portable)) {
                    Entity toAdd = (Entity) portable;
                    toAdd.map = this.map;
                    if (toAdd.setPosition(selectedCell)) {
                        this.backpack.removePortable(portable);
                        placed = true;
                    } else {
                        toAdd.map = null;
                    }
                }
            }
        }

        return placed;
    }

    public SpaceSuit getSuit ( ) {
        return this.suit;
    }

    public Texture getTexture ( ) {
        return this.texture;
    }

    public boolean refillContainers (Cell selectedCell) {

        boolean success = false;

        for (Entity entity : selectedCell.getEntities()) {
            // Can't refill yourself!
            if (this.equals(entity)) {
                continue;
            }
            for (FluidContainer myContainer : this.containers) {
                for (FluidContainer theirContainer : entity.getContainers()) {
                    if (myContainer.getFluidType().equals(theirContainer.getFluidType())) {
                        myContainer.refillFluid(theirContainer);
                        success = true;
                        // We only want to refill each myContainer once per tick!
                        break;
                    }
                }
            }
        }

        return success;
    }
}
